package com.J1ay.carsys.service.Impl;

import com.J1ay.carsys.dao.BowCarMapper;
import com.J1ay.carsys.dao.CarMapper;
import com.J1ay.carsys.pojo.BowCar;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.UserService;
import com.J1ay.carsys.utils.Common;
import com.J1ay.carsys.utils.MybatisUtils;
import org.apache.ibatis.exceptions.PersistenceException;

/**
 * @Author J1ay
 * @Date 2021/6/3 20:49
 * @Description
 */

public class UserServiceImpl implements UserService {
    @Override
    public Result getUnBowCar() {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(CarMapper.class).getUnBowCar());
    }

    @Override
    public Result getCarByUId(String UId) {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(BowCarMapper.class).selectByUId(UId));
    }

    @Override
    public Result addRecord(BowCar bowCar){
        try {
            MybatisUtils.getSqlSession().getMapper(BowCarMapper.class).addRecord(Common.getMapperFactory().getMapperFacade().map(bowCar, BowCar.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result updateRecord(BowCar bowCar){
        try {
            MybatisUtils.getSqlSession().getMapper(BowCarMapper.class).updateRecord(Common.getMapperFactory().getMapperFacade().map(bowCar, BowCar.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }
}
